﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TelecoSystems.Modules.Contracts.ViewModels;

namespace TelecoSystems.Modules.Contracts.Views
{
    public partial class ContractsBrowseView : UserControl
    {
        public ContractsBrowseView(ContractsBrowseViewModel _viewModel)
        {
            InitializeComponent();

            DataContext = _viewModel;            
            _viewModel.ContractAdded += (s, e) => {
                CustomerDomainDataSource.Load();
                ProductDomainDataSource.Load();                
            };

            customerNameComboBox.SelectionChanged += (s, e) => {
                try {
                    _viewModel.ContractDetail.CustomerId = (int) ((ComboBox)s).SelectedValue;
                } catch {
                    //
                }
            };

            variantNameComboBox.SelectionChanged += (s, e) => {
                try {
                    _viewModel.ContractDetail.VariantId = (int) ((ComboBox)s).SelectedValue;
                } catch { 
                    //
                }
            };
        }

        private void contractPMDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e) {
            if (e.HasError) {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void customerDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e) {
            if (e.HasError) {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void productDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e) {
            if (e.HasError) {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void variantDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e) {
            if (e.HasError) {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void productNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            VariantDomainDataSource.Clear();
            VariantDomainDataSource.Load();            
        }

        private void VariantDomainDataSource_LoadingData(object sender, LoadingDataEventArgs e) {            
            e.LoadBehavior = System.ServiceModel.DomainServices.Client.LoadBehavior.MergeIntoCurrent;
        }
    }
}
